package com.swpu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HelloServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String cheRem = request.getParameter("cheRem");
        if (cheRem.equals("1")){
            Cookie cookie = new Cookie("name",username);
            cookie.setMaxAge(7*24*60*60);
            response.addCookie(cookie);
        }

        //Session保存对象
        request.getSession().setAttribute("name",username);
        //读取值
        request.getSession().getAttribute("name");

        //设置session默认存在时间
        request.getSession().setMaxInactiveInterval(40);
    }
}
